package com.campusbackend.service.impl;

import com.campusbackend.entity.EducationBackgrounds;
import com.campusbackend.entity.vo.EducationCreateVO;
import com.campusbackend.entity.vo.EducationUpdateVO;
import com.campusbackend.mapper.EducationBackgroundsMapper;
import com.campusbackend.service.EducationBackgroundsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 教育背景表 服务实现类
 * </p>
 *
 * @author yezi
 * @since 2025-07-24
 */
@Service
public class EducationBackgroundsServiceImpl extends ServiceImpl<EducationBackgroundsMapper, EducationBackgrounds> implements EducationBackgroundsService {

    @Autowired
    private EducationBackgroundsMapper educationBackgroundsMapper;

    @Override
    public List<EducationBackgrounds> getStudentEducations(Long studentId) {
        return educationBackgroundsMapper.selectByStudentId(studentId);
    }

    @Override
    public String addStudentEducation(Long studentId, EducationCreateVO createVO) {
        try {
            EducationBackgrounds education = new EducationBackgrounds();
            BeanUtils.copyProperties(createVO, education);
            
            // 设置学生ID
            education.setStudentId(studentId);
            
            // 设置时间
            education.setCreateTime(LocalDateTime.now());
            education.setUpdateTime(LocalDateTime.now());
            
            boolean saved = save(education);
            if (saved) {
                return "教育背景添加成功";
            } else {
                return "教育背景添加失败，请稍后重试";
            }
        } catch (Exception e) {
            return "添加教育背景时发生错误：" + e.getMessage();
        }
    }

    @Override
    public String updateStudentEducation(Long studentId, EducationUpdateVO updateVO) {
        try {
            // 首先检查该教育背景记录是否属于当前学生
            EducationBackgrounds existingEducation = educationBackgroundsMapper.selectByIdAndStudentId(updateVO.getId(), studentId);
            if (existingEducation == null) {
                return "教育背景记录不存在或无权限访问";
            }
            
            // 更新教育背景信息
            BeanUtils.copyProperties(updateVO, existingEducation, "studentId", "createTime");
            existingEducation.setUpdateTime(LocalDateTime.now());
            
            boolean updated = updateById(existingEducation);
            if (updated) {
                return "教育背景更新成功";
            } else {
                return "教育背景更新失败，请稍后重试";
            }
        } catch (Exception e) {
            return "更新教育背景时发生错误：" + e.getMessage();
        }
    }

    @Override
    public String deleteStudentEducation(Long studentId, Long educationId) {
        try {
            // 首先检查该教育背景记录是否属于当前学生
            EducationBackgrounds existingEducation = educationBackgroundsMapper.selectByIdAndStudentId(educationId, studentId);
            if (existingEducation == null) {
                return "教育背景记录不存在或无权限访问";
            }
            
            boolean deleted = removeById(educationId);
            if (deleted) {
                return "教育背景删除成功";
            } else {
                return "教育背景删除失败，请稍后重试";
            }
        } catch (Exception e) {
            return "删除教育背景时发生错误：" + e.getMessage();
        }
    }
}